/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import java.util.LinkedHashSet;

import be.destin.rdf.changes.Status;


/**
 * @author Christophe Dupriez
 *
 */

public class UrlList extends LinkedHashSet<Url> {

	public static final long serialVersionUID = 1;
	public static final String anyLanguage = "";

	/**
	 * add an Url if it is not already completely/exactly there
	 * @param language: the language of the Url to be added if necessary
	 * @param aValue: the template/link text for that Url
	 */
	public void addNew (String language, String aValue) {
		Url anUrl = Url.newValid(language,aValue);
		if (anUrl == null) return;
		addNew(anUrl);
	}

	/**
	 * add an Url if it is not already completely/exactly there
	 * @param anUrl: the Url to be added if necessary
	 */
	public void addNew (Url anUrl) {
		if (!this.contains(anUrl)) {
			this.add(anUrl);
		}
	}

	/**
	 * @param language of the user
	 * @return Url template/link with the best fit for the requested language
	 */
	public String getLing(String language) {
		if (this.size() <= 0) return null;
		if (language == null || language.length()==0) language = anyLanguage;
		String ownLanguage = null;
		String english = null;
		String anAny = null;
		String aValue = null;
		for (Url anEntry : this) {
			String aLang = anEntry.getLang();
			aValue = anEntry.getValue();
			if (aValue == null) continue;
			if (aLang == null) anAny = aValue;
			else if (aLang.equals(anyLanguage)) anAny = aValue;
			else if (aLang.startsWith(language)) ownLanguage = aValue;
			else if (aLang.startsWith(TermList.ENGLISH)) english = aValue;
		}
		if (ownLanguage != null) return ownLanguage;
		if (english != null) return english;
		if (anAny != null) return anAny;
		return aValue;
	}

	/**
	 * Set the entry for a given language (fields with unique values for a language)
	 * @param language
	 * @param aValue
	 * @return the assigned Url
	 */
	public Url setLing(String language, String aValue) {
		if (aValue == null) return null;
		if (language == null) language = TermList.anyLanguage;
		for (Url anEntry : this) {
			String aLang = anEntry.getLang();
			if (aLang.equals(language)) {
				anEntry.setValue(aValue);
				return anEntry;
			}
			else if ( language != anyLanguage && aLang.startsWith(language)) {
				anEntry.setValue(aValue);
				return anEntry;
			}
		}
		Url anEntry = new Url(language,aValue);
		this.add(anEntry);
		return anEntry;
	}
	
	public Url setLing(Url aUrl) {
		if (aUrl == null) return null;
		Url currUrl = this.setLing(aUrl.getLang(),aUrl.getLabel());
		Status newStatus = aUrl.getCurrentStatus();
		//if (newStatus != null) {
		currUrl.setCurrentStatus(newStatus);
		//}
		return currUrl;
	}
	
	public void merge (UrlList source) {
		if (source != null) {
			for (Url anUrl : source) {
				Url anEntry = setLing(anUrl.getLang(),anUrl.getValue());
				Status newStatus = anUrl.getCurrentStatus();
				//if (newStatus != null) {
				anEntry.setCurrentStatus(newStatus);
				//}
			}
		}
	}
	
}
